package com.rsscollector.controller.admin.form;

import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;


public class CatalogForm extends ActionForm {
    private String selectedNodeId;
    private String parentSelectedNodeId;
    private String newCategory;
    private String newTitle;
    private String newUrl;
    private String newUrlProvider;
    private String action;

    public String getAction() {
		return this.action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getSelectedNodeId() {
		return this.selectedNodeId;
	}

	public void setSelectedNodeId(String selectedNodeId) {
		this.selectedNodeId = selectedNodeId;
	}

	public String getNewCategory() {
		return this.newCategory;
	}

	public void setNewCategory(String newCategory) {
		this.newCategory = newCategory;
	}

    public String getNewTitle() {
        return newTitle;
    }

    public void setNewTitle(String newTitle) {
        this.newTitle = newTitle;
    }
    
    public String getNewUrl() {
		return this.newUrl;
	}

	public void setNewUrl(String newUrl) {
		this.newUrl = newUrl;
	}

	public String getNewUrlProvider() {
		return this.newUrlProvider;
	}

	public void setNewUrlProvider(String newUrlProvider) {
		this.newUrlProvider = newUrlProvider;
	}
	
	public String getParentSelectedNodeId() {
		return this.parentSelectedNodeId;
	}

	public void setParentSelectedNodeId(String parentSelectedNodeId) {
		this.parentSelectedNodeId = parentSelectedNodeId;
	}

	/**
     * Reset all properties to their default values.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    @Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.selectedNodeId = null;
        this.parentSelectedNodeId = null;
        this.newCategory = null;
        this.newUrl = null;
        this.newUrlProvider = null;
        this.action = null;
    }

    /**
     * Validate the properties that have been set from this HTTP request, and
     * return an <code>ActionErrors</code> object that encapsulates any
     * validation errors that have been found. If no errors are found, return
     * <code>null</code> or an <code>ActionErrors</code> object with no
     * recorded error messages.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    @Override
	public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();
        
        if (this.selectedNodeId == null || "".equals(this.selectedNodeId.trim())) {
        	errors.add("selectedNodeId", new ActionMessage("admin.Catalog.selectedCategory.required"));
        }
        if ("Create Category".equals(this.action)) {
        	if (this.newCategory == null || "".equals(this.newCategory)) {
        		errors.add("newCategory", new ActionMessage("admin.Catalog.newCategory.required"));
        	}
            if (this.newTitle == null || "".equals(this.newTitle)) {
                errors.add("newTitle", new ActionMessage("admin.Catalog.newTitle.required"));
            }
        }
        if ("Create URL".equals(this.action)) {
        	if (this.newUrl == null || "".equals(this.newUrl)) {
        		errors.add("newUrl", new ActionMessage("admin.Catalog.newUrl.required"));
        	} else {
        	    try {
        	        new URL(this.newUrl);
        	    } catch (MalformedURLException e) {
            		errors.add("newUrl", new ActionMessage("admin.Catalog.newUrl.required"));
        	    }
        	}
        	if (this.newUrlProvider == null || "".equals(this.newUrlProvider)) {
        		errors.add("newUrlProvider", new ActionMessage("admin.Catalog.newUrlProvider.required"));
        	}
        }
        return (errors);

    }
}
